package com.zhaokuner.ctrl;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.config.AlipayConfig;
import com.sevenchildren.framework.util.DateUtils;

@Controller
@RequestMapping("/tt")
public class AlipayCtrl {

	@RequestMapping("pay")
	public String pay(Model model, HttpServletResponse response) throws IOException {
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); // 获得初始化的AlipayClient
		// AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();// 创建API对应的request
		// alipayRequest.setReturnUrl(AlipayConfig.RETURN_URL);
		// alipayRequest.setNotifyUrl(AlipayConfig.NOTIFY_URL);// 在公共参数中设置回跳和通知地址
		// // 封装请求支付信息
		// AlipayTradeWapPayModel tradeModel = new AlipayTradeWapPayModel();
		// tradeModel.setOutTradeNo(DateUtils.getWsTime()); // 商户订单号，商户网站订单系统中唯一订单号，必填
		// tradeModel.setSubject("Iphone6 16G"); // 订单名称，必填
		// tradeModel.setTotalAmount("8.88");// 付款金额，必填
		// tradeModel.setBody("【苹果手机】"); // 商品描述，可空
		// tradeModel.setTimeoutExpress("2m"); // 超时时间 可空
		// tradeModel.setProductCode("QUICK_WAP_PAY"); // 销售产品码 必填 手机H5
		// alipayRequest.setBizModel(tradeModel);

		AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();// 创建API对应的request
		alipayRequest.setReturnUrl(AlipayConfig.RETURN_URL);
		alipayRequest.setNotifyUrl(AlipayConfig.NOTIFY_URL);// 在公共参数中设置回跳和通知地址
		AlipayTradePagePayModel tradeModel = new AlipayTradePagePayModel();
		tradeModel.setOutTradeNo(DateUtils.getWsTime()); // 商户订单号，商户网站订单系统中唯一订单号，必填
		tradeModel.setSubject("Iphone6 16G"); // 订单名称，必填
		tradeModel.setTotalAmount("8.88");// 付款金额，必填
		tradeModel.setBody("【苹果手机】"); // 商品描述，可空
		tradeModel.setTimeoutExpress("2m"); // 超时时间 可空
		tradeModel.setProductCode("FAST_INSTANT_TRADE_PAY"); // 销售产品码 必填 手机H5
		alipayRequest.setBizModel(tradeModel);

		String form = "";
		try {
			form = alipayClient.pageExecute(alipayRequest).getBody(); // 调用SDK生成表单
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		response.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
		response.getWriter().write(form);// 直接将完整的表单html输出到页面
		response.getWriter().flush();
		response.getWriter().close();
		return null;
	}

}
